//
//  TestTableViewController.m
//  FastDoctor
//
//  Created by 李通 on 16/7/7.
//  Copyright © 2016年 sc. All rights reserved.
//

#import "PersonCenteViewController.h"
#import <UIImageView+WebCache.h>
#import "SelectCityController.h"
#import "ImageCell.h"
#import "UIImage+Scale.h"
#define kFilePath(fileName) ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:fileName])

@interface PersonCenteViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic,strong) NSDictionary *personInfo;

@property (nonatomic,strong) UIImage *image;

@end

@implementation PersonCenteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.personInfo = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"user.data")];
    if ([[NSUserDefaults standardUserDefaults]objectForKey:@"cicty"] == nil) {
        [[NSUserDefaults standardUserDefaults]setObject:self.personInfo[@"address"] forKey:@"cicty"];
    }
    self.view.backgroundColor = WHITE_COLOR;
    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 5;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        
        cell = [[ImageCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    if (indexPath.row == 0) {
        NSString *str =self.personInfo[@"true_name"];
        NSString *str1 =[self.personInfo[@"gender"] integerValue] ? @"男" : @"女";
        cell.textLabel.text =[NSString stringWithFormat:@"%@ %@",str,str1];
        UIImage *image = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"image.data")];
        if (image) {
            cell.headImageView.image = image;
        }else{
            if (!self.image) {
                
                [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:self.personInfo[@"head_photo"]] placeholderImage:[UIImage imageNamed:@"name"]];
            }else{
                cell.headImageView.image = self.image;
            }
        }
    }
    if (indexPath.row == 1) {
        
        cell.textLabel.text = self.personInfo[@"card_type"] ? @"身份证" : @"其他证件";
        cell.detailTextLabel.text = self.personInfo[@"card_number"];
    }
    if (indexPath.row == 2) {
        
        cell.textLabel.text = @"手机号码";
        cell.detailTextLabel.text = self.personInfo[@"mobile_number"];
    }
    if (indexPath.row == 3) {
        
        cell.textLabel.text = @"籍贯地址";
        cell.detailTextLabel.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"cicty"];
    }
    if (indexPath.row == 4) {
        
        cell.textLabel.text = @"年龄";
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld",[self.personInfo[@"age"] integerValue]];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        return 100;
    }
    return 60;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
        
        pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        
        pickerController.delegate = self;
        
        [self presentViewController:pickerController animated:YES completion:nil];
        
    }else if(indexPath.row == 3){
        SelectCityController *vc = [[SelectCityController alloc]init];
        vc.title = @"请选择籍贯地址";
        vc.block = ^{
            [self.tableView reloadData];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
}

    
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{

        
        UIImage *image = info[UIImagePickerControllerOriginalImage];
        image = [image scaleToSize:CGSizeMake(80, 80)];
        self.image = image;
        [NSKeyedArchiver archiveRootObject:self.image toFile:kFilePath(@"image.data")];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"upload" object:nil];
        [self.tableView reloadData];
        
        [self dismissViewControllerAnimated:YES completion:nil];
}
@end
